package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.UserAndComment;
import com.woniu.entity.dto.CommentDTO;
import com.woniu.entity.vo.CommentVO;
import com.woniu.entity.vo.CommenteVO;
import com.woniu.mapper.CommentMapper;
import com.woniu.pojo.*;
import com.woniu.service.ICommentService;
import com.woniu.service.ILikeService;
import com.woniu.service.IMessageService;
import com.woniu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ILikeService likeService;
    @Autowired
    private IMessageService messageService;
//    分页查询
    @Override
    public PageData search(Integer currentPage, Integer pageSize, CommentDTO commentDTO) {

//        开启分页
        IPage<CommenteVO> page = new Page<>(currentPage,pageSize);
        IPage<CommenteVO> commentVOS = commentMapper.selectall(page,commentDTO);



//         封装PageData对象
        PageData pageData = new PageData(commentVOS.getTotal(),commentVOS.getRecords());


        return pageData;

    }
//按照id查询
    @Override
    public Comment selectById(Integer id) {

        final Comment comment = commentMapper.selectById(id);
        return comment;
    }

    @Override
    public Integer updateComment(Comment comment) {

        return commentMapper.updateById(comment);
    }

    public List<CommentVO> listRootComment(String trainingId) {
        List<CommentVO> commentVOList = new ArrayList<>();
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        QueryWrapper<Comment> rootCommentQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Like> likeWrapper = new QueryWrapper<>();
        wrapper.eq("root_parentId",0);
        wrapper.eq("training_id",trainingId);
        List<Comment> rootCommentList = commentService.list(wrapper);
        for (Comment rootComment:rootCommentList) {
            Integer rootUserId = rootComment.getUserId();
            Integer rootId = rootComment.getId();
            User rootUser = userService.getById(rootUserId);
            likeWrapper.eq("comment_id",rootComment.getId());
            Integer rootLike =likeService.list(likeWrapper).size();
            CommentVO commentVO = new CommentVO();
            UserAndComment rootUserAndComment = new UserAndComment();
            rootUserAndComment.setLike(rootLike);
            likeWrapper.clear();
            rootUserAndComment.setUser(rootUser);
            rootUserAndComment.setComment(rootComment);
            commentVO.setUserAndComment(rootUserAndComment);
            rootCommentQueryWrapper.eq("root_parentId",rootId);
            List<Comment> commentList = commentService.list(rootCommentQueryWrapper);
            rootCommentQueryWrapper.clear();
            List<UserAndComment> userAndCommentList = new ArrayList<>();
            for (Comment comment: commentList) {
                Integer id = comment.getUserId();
                User user = userService.getById(id);
                likeWrapper.eq("comment_id",comment.getId());
                Integer like =likeService.list(likeWrapper).size();
                UserAndComment userAndComment = new UserAndComment();
                userAndComment.setLike(like);
                likeWrapper.clear();
                userAndComment.setUser(user);
                userAndComment.setComment(comment);
                userAndCommentList.add(userAndComment);
            }
            commentVO.setCommentList(userAndCommentList);
            commentVOList.add(commentVO);
        }
        return commentVOList;
    }

    @Override
    public boolean add(Comment comment) {
        commentService.save(comment);
       if (comment.getCommentParentid() != 0) {
            Comment parentComment = commentService.getById(comment.getCommentParentid());
            User receiveUser = userService.getById(parentComment.getUserId());
            User sendUser = userService.getById(comment.getUserId());
            Message message = new Message();
            message.setUserId(receiveUser.getId());
            message.setUserName(receiveUser.getName());
            message.setTitle("您收到了来自'" + sendUser.getName() + "'用户的评论");
            message.setContent("您的'" + parentComment.getMessage() + "'评论收到了来自'" + sendUser.getName() + "'用户的评论，评论内容为'"+comment.getMessage()+"'");
            message.setType((byte) 1);
            message.setStatus((byte) 0);
            messageService.save(message);
        }
        return true;
    }

    @Override
    public Integer getUnread(Integer id) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.eq("type",1);
        wrapper.eq("status",0);
        Integer unreadComment = messageService.list(wrapper).size();
        return unreadComment;
    }

    @Override
    public List<CommentVO> listRootCommentByTime(String trainingId) {
        List<CommentVO> commentVOList = new ArrayList<>();
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        QueryWrapper<Comment> rootCommentQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Like> likeWrapper = new QueryWrapper<>();
        wrapper.eq("root_parentId",0);
        wrapper.eq("training_id",trainingId);
        wrapper.orderByDesc("create_time");
        List<Comment> rootCommentList = commentService.list(wrapper);
        for (Comment rootComment:rootCommentList) {
            Integer rootUserId = rootComment.getUserId();
            Integer rootId = rootComment.getId();
            User rootUser = userService.getById(rootUserId);
            likeWrapper.eq("comment_id",rootComment.getId());
            Integer rootLike =likeService.list(likeWrapper).size();
            CommentVO commentVO = new CommentVO();
            UserAndComment rootUserAndComment = new UserAndComment();
            rootUserAndComment.setLike(rootLike);
            likeWrapper.clear();
            rootUserAndComment.setUser(rootUser);
            rootUserAndComment.setComment(rootComment);
            commentVO.setUserAndComment(rootUserAndComment);
            rootCommentQueryWrapper.eq("root_parentId",rootId);
            List<Comment> commentList = commentService.list(rootCommentQueryWrapper);
            rootCommentQueryWrapper.clear();
            List<UserAndComment> userAndCommentList = new ArrayList<>();
            for (Comment comment: commentList) {
                Integer id = comment.getUserId();
                User user = userService.getById(id);
                likeWrapper.eq("comment_id",comment.getId());
                Integer like =likeService.list(likeWrapper).size();
                UserAndComment userAndComment = new UserAndComment();
                userAndComment.setLike(like);
                likeWrapper.clear();
                userAndComment.setUser(user);
                userAndComment.setComment(comment);
                userAndCommentList.add(userAndComment);
            }
            commentVO.setCommentList(userAndCommentList);
            commentVOList.add(commentVO);
        }
        return commentVOList;
    }

    @Override
    public List<CommentVO> listRootCommentByLike(String trainingId) {
        List<CommentVO> commentVOList = new ArrayList<>();
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        QueryWrapper<Comment> rootCommentQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Like> likeWrapper = new QueryWrapper<>();
        wrapper.eq("root_parentId",0);
        wrapper.eq("training_id",trainingId);
        List<Comment> rootCommentList = commentService.list(wrapper);
        //搜索出所有的最顶评论集合
        for (Comment rootComment:rootCommentList) {
            Integer rootUserId = rootComment.getUserId();
            Integer rootId = rootComment.getId();
            User rootUser = userService.getById(rootUserId);
            likeWrapper.eq("comment_id",rootComment.getId());
            Integer rootLike =likeService.list(likeWrapper).size();
            CommentVO commentVO = new CommentVO();
            UserAndComment rootUserAndComment = new UserAndComment();
            rootUserAndComment.setLike(rootLike);
            likeWrapper.clear();
            rootUserAndComment.setUser(rootUser);
            rootUserAndComment.setComment(rootComment);
            commentVO.setUserAndComment(rootUserAndComment);
            //为每个最顶评论绑定各自的副属性
            rootCommentQueryWrapper.eq("root_parentId",rootId);
            List<Comment> commentList = commentService.list(rootCommentQueryWrapper);
            rootCommentQueryWrapper.clear();
            List<UserAndComment> userAndCommentList = new ArrayList<>();
            for (Comment comment: commentList) {
                Integer id = comment.getUserId();
                User user = userService.getById(id);
                likeWrapper.eq("comment_id",comment.getId());
                Integer like =likeService.list(likeWrapper).size();
                UserAndComment userAndComment = new UserAndComment();
                userAndComment.setLike(like);
                likeWrapper.clear();
                userAndComment.setUser(user);
                userAndComment.setComment(comment);
                userAndCommentList.add(userAndComment);
                //为每个最顶评论绑定各自的副评论
            }
            commentVO.setCommentList(userAndCommentList);
            commentVOList.add(commentVO);
        }
        commentVOList.sort(new Comparator<CommentVO>() {
            @Override
            public int compare(CommentVO o1, CommentVO o2) {
                return o2.getUserAndComment().getLike()-o1.getUserAndComment().getLike();
            }
        });
        return commentVOList;
    }

}
